package com.kin.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kin.pojo.Group;
import com.kin.service.IGroupService;
import com.kin.tools.IToolsService;

/**
 * 角色信息Controller
 * @author lauvinson
 * @time 2017年7月14日09:37:51
 */
@Controller("groupController")
@SessionAttributes("groupid")
public class GroupController {
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private IGroupService groupService;//角色Service
	@Resource
	private IToolsService toolsService;//工具Service
	@Resource
	UserController userController;//用户Service
	
	
	/**
	 * 获取角色名称(POST)
	 * @param groupid 角色id
	 * @return msg:groupname,status:fail
	 */
	@RequestMapping(value="getGroupName",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject getGroupName(@ModelAttribute("groupid") String groupid)
	{
		try{
		Group groupName =  groupService.getGroupById(Integer.parseInt(groupid));
		return toolsService.stringToJsonObject("{'msg':'"+groupName.getGroupname()+"'}");
		}
		catch(Exception e)
		{
		return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
		}
	}
	
	/**
	 * 获取所有用户组信息
	 * @return (json)Group
	 */
	@RequestMapping(value="/getAllGroup",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getAllGroup()
	{
		try
		{
			JSONObject groupall = toolsService.stringToJsonObject(JSON.toJSONString(groupService.getAllGroupInfo()));
			groupall.put("status", "success");
			return groupall;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return toolsService.stringToJsonObject(UserController.STATUS_FAIL);
		}
	}
	
	
	/**
	 * 角色初始化id
	 * @return 000s
	 */
	@ModelAttribute("groupid")
	public String getUser()
	{
		return "000";
	}
}
